const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

/**
 * 支付记录模型
 */
const Payment = sequelize.define('Payment', {
  id: {
    type: DataTypes.STRING(64),
    primaryKey: true,
    comment: '支付ID'
  },
  user_id: {
    type: DataTypes.UUID,
    allowNull: false,
    comment: '用户ID'
  },
  business_type: {
    type: DataTypes.ENUM('order', 'recharge'),
    allowNull: false,
    comment: '业务类型：order-订单，recharge-充值'
  },
  business_id: {
    type: DataTypes.UUID,
    allowNull: false,
    comment: '业务ID（订单ID或充值ID）'
  },
  payment_type: {
    type: DataTypes.ENUM('wechat', 'balance', 'mock'),
    allowNull: false,
    comment: '支付方式：wechat-微信支付，balance-余额支付，mock-模拟支付'
  },
  amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
    comment: '支付金额'
  },
  status: {
    type: DataTypes.ENUM('pending', 'success', 'failed'),
    allowNull: false,
    defaultValue: 'pending',
    comment: '支付状态：pending-处理中，success-成功，failed-失败'
  },
  create_time: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  update_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '更新时间'
  },
  remark: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: '备注'
  }
}, {
  tableName: 'payments',
  timestamps: false,
  indexes: [
    { fields: ['user_id'] },
    { fields: ['business_id'] },
    { fields: ['business_type', 'business_id'] }
  ]
});

module.exports = Payment; 